package com.soft.one;

import com.soft.one.db.UserDb;
import com.soft.one.entity.User;
import com.soft.one.exception.MyException;
import com.soft.one.myprintf.MyPrintfs;

import java.util.List;
import java.util.Scanner;

public class LoginService {

    private static User loginUser = null;

    public boolean login() {
        Scanner sc = new Scanner(System.in);
        MyPrintfs.loginPrintf();
        System.out.println("请输入账号：");
        String username = sc.nextLine();
        System.out.println("请输入密码：");
        String password = sc.nextLine();
        //登录后
        boolean b = checkUser(username, password);
        return b;
    }

    private static boolean checkUser(String username, String password) {
        List<User> userList = UserDb.getUserList();
        if(userList.isEmpty()) {
            throw new MyException("用户列表为空");
        }
        for (User user : userList) {
            //账号密码正确
            if(username.equals(user.getUsername()) && password.equals(user.getPassword())){

                loginUser = user;
                return true;
            }
        }
        return false;
    }

    public static void loginInfo() {

        System.out.println("&&&&&&&&&&&&&&&&&&&&&&&&");
        System.out.println(loginUser.getUsername());
        System.out.println(loginUser.getPhone());
        System.out.println(loginUser.getEmail());
        System.out.println(loginUser.getDesc());
        System.out.println("&&&&&&&&&&&&&&&&&&&&&&&&");
    }
}
